package com.linran.poll.service.loop;

import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.Callable;

@Slf4j
public class LoopPushTask implements Callable<String> {

    private LoopPullTask loopPullTask;
    private String data;
    private Object lock;

    public LoopPushTask(LoopPullTask loopPullTask, String data) {
        this.loopPullTask = loopPullTask;
        this.data = data;
    }

    public LoopPushTask(LoopPullTask loopPullTask, String data, Object lock) {
        this.loopPullTask = loopPullTask;
        this.data = data;
        this.lock = lock;
    }

    /*callable办法传参,可以通过构造器进行传参*/

    /*方式一*/
//    @Override
//    public String call() throws Exception {
//        loopPullTask.setData(this.data);
//        return "changed";
//    }

    /*方式二*/
    @Override
    public String call() throws Exception {
        log.info("数据发生变更:{}", LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        loopPullTask.setData(this.data);
        synchronized (lock) {

        }
        return "changed";
    }
}
